<?php
/**
 * @desc 游客管理数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Passenger extends Base
{
	//数据表
	protected $table = 'pert_passenger_info';
	//查询游客列表
	public function getPassengerList(array $con)  
    {
		 $model = new Order;
		 $order = $model->getOrder($con['pid']);
		 $table = $this->table;
		 $filed = "`passenger_info_id`,`user_id`,`order_sn`,`passenger_name`,`passenger_first`,`passenger_second`,`sex`,`certification_type`,`certification_number`,`cartification_date`,`birthday`,`mobile`,`raw_add_time`,`raw_update_time`";
		 $sql = "select {$filed} from `{$table}` where `delete_flag` = 0 and `order_sn` = :order_sn ";
		 $where = "";
		 if(!empty($con['username'])){
			 $where .= " and `passenger_name` like '%{$con['username']}%' ";
		 }
		 if(!empty($con['mobile'])){
			 $where .= " and `mobile` like '%{$con['mobile']}%' ";
		 }
		 $limit = " order by `passenger_info_id` desc limit ".(($con['current'] - 1)*$con['numPerPage']).", ".$con['numPerPage'];
		 $return = $this->select_table($sql.$where.$limit, array(':order_sn'=>$order['mainOrder']['order_sn']));
		 $sql = "select count(`passenger_info_id`) as num from `{$table}` where `delete_flag` = 0 and `order_sn` = :order_sn";
		 $return[] = $this->select_table($sql.$where, array(':order_sn'=>$order['mainOrder']['order_sn']), Base::QUERY_COLUMN);
		 return $return;
    }
	//删除用户
	public function deletePassenger($ids){
		$table = $this->table;
		$sql = "update `{$table}` set `delete_flag` = 1 where `passenger_info_id` in ({$ids})";
		return $this->update_table($sql, array());
	}
	//写入用户
	public function insertUser(array $data){
		if(empty($data)){
			return false;
		}
		return self::insert_table($data, $this->table);
	}
	//获取用户
	public function getUser($id){
		$table = $this->table;
		$filed = "`user_id`,`username`,`nick_name`,`mobile`,`email`,`login_count`,`status`,`raw_add_time`";
		$sql = "select {$filed} from `{$table}` where `user_id` = :user_id and `delete_flag` = 0";
		return $this->select_table($sql, array(':user_id'=>$id), Base::QUERY_RAW);
	}
	//修改用户
	public function editUser($id, array $data){
		if(empty($id) || empty($data)){
			return false;
		}
		$table = $this->table;
		$sql = "update `{$table}` set ";
		$binds = array();
		foreach($data as $key=>$value){
			$sql .= "`{$key}` = :{$key},";
			$binds[":{$key}"] = $value;
		}
		$sql = trim($sql, ',');
		$sql .= " where `user_id` = :user_id";
		$binds[':user_id'] = $id;
		return $this->update_table($sql, $binds);
	}
}
